<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="发送时间" prop="stime">
        <el-date-picker
          v-model="dataForm.stime"
          type="date"
          placeholder="可选参数。发送时间，填写时已填写的时间发送，不填时为当前时间发送">
        </el-date-picker>
      </el-form-item>
    <el-form-item label="发送内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="必填参数。发送内容（1-500 个汉字）UTF-8编码"></el-input>
    </el-form-item>
    <el-form-item label="手机号码" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="必填参数。手机号码。多个以英文逗号隔开"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          content: '',
          mobile: '',
          stime: '',
          sign: '',
          type: '',
          extno: '',
          sendStatus: '',
          sendId: '',
          invalidNum: '',
          successNum: '',
          blackNum: '',
          returnMsg: ''
        },
        dataRule: {
          content: [
            {required: true, message: '必填参数。发送内容（1-500 个汉字）UTF-8编码不能为空', trigger: 'blur'}
          ],
          mobile: [
            {required: true, message: '必填参数。手机号码。多个以英文逗号隔开不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/syssmslog/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.syssmslog.userId
                this.dataForm.content = data.syssmslog.content
                this.dataForm.mobile = data.syssmslog.mobile
                this.dataForm.stime = data.syssmslog.stime
                this.dataForm.sign = data.syssmslog.sign
                this.dataForm.type = data.syssmslog.type
                this.dataForm.extno = data.syssmslog.extno
                this.dataForm.sendStatus = data.syssmslog.sendStatus
                this.dataForm.sendId = data.syssmslog.sendId
                this.dataForm.invalidNum = data.syssmslog.invalidNum
                this.dataForm.successNum = data.syssmslog.successNum
                this.dataForm.blackNum = data.syssmslog.blackNum
                this.dataForm.returnMsg = data.syssmslog.returnMsg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/syssmslog/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'content': this.dataForm.content,
                'mobile': this.dataForm.mobile,
                'stime': this.dataForm.stime,
                'sign': this.dataForm.sign,
                'type': this.dataForm.type,
                'extno': this.dataForm.extno,
                'sendStatus': this.dataForm.sendStatus,
                'sendId': this.dataForm.sendId,
                'invalidNum': this.dataForm.invalidNum,
                'successNum': this.dataForm.successNum,
                'blackNum': this.dataForm.blackNum,
                'returnMsg': this.dataForm.returnMsg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
